﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Douya.Json
{
    public class JsonArray : IJsonValue
    {
        List<IJsonValue> lst;

        public JsonArray(List<IJsonValue> lst)
        {
            this.lst = lst;
        }

        //--------------------------------------------------------------------------------------------------------------
        // IJsonValue

        public ValueType Type
        {
            get { return ValueType.TypeArray; }
        }

        public bool GetBoolean()
        {
            return lst.Count > 0;
        }

        public int GetInteger()
        {
            return lst.Count;
        }

        public string GetString()
        {
            return "array";
        }

        public JsonObject GetObject()
        {
            return null;
        }

        public JsonArray GetArray()
        {
            return this;
        }

        //--------------------------------------------------------------------------------------------------------------
        // JsonArray

        public int Count
        {
            get { return lst.Count; }
        }

        public IJsonValue GetValue(int index)
        {
            return lst[index];
        }

        public bool GetBoolean(int index)
        {
            return lst[index].GetBoolean();
        }

        public int GetInteger(int index)
        {
            return lst[index].GetInteger();
        }

        public string GetString(int index)
        {
            return lst[index].GetString();
        }

        public JsonObject GetObject(int index)
        {
            return lst[index].GetObject();
        }

        public JsonArray GetArray(int index)
        {
            return lst[index].GetArray();
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            foreach (IJsonValue value in lst)
            {
                builder.AppendFormat("{0},", value.ToString());
            }
            if (builder.Length > 1)
            {
                --builder.Length;
            }
            builder.Append("]");
            return builder.ToString();
        }
    }
}
